home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVPCFB.C < prev    next >
C/C++ Source or Header  |  1992-02-21  |  27KB  |  993 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevpcfb.c */
  21. /* IBM PC EGA and VGA display drivers for Ghostscript */
  22. #include "dos_.h"
  23. typedef union REGS registers;
  24. /* This is fundamentally an EGA driver with some parameters */
  25. /* that allow it to drive larger displays. */
  26. #include "memory_.h"
  27. #include "gs.h"
  28. #include "gxdevice.h"
  29.  
  30. #ifndef USE_ASM
  31. #  define USE_ASM 0            /* don't use assembly language */
  32. #endif
  33.  
  34. /* Define the short (integer) version of "transparent" color. */
  35. /* ****** Depends on gx_no_color_index being all 1's. ******/
  36. #define no_color ((int)gx_no_color_index)
  37.  
  38. /* For testing, the EGA may be defined as a monochrome, 8-color, or */
  39. /* 16-color device. */
  40. #define ega_bits_of_color 2        /* 0, 1, or 2 */
  41.  
  42. /* Range of r-g-b values */
  43. #define rgb_max ega_bits_of_color
  44.  
  45. /* Procedures */
  46.  
  47.     /* See gxdevice.h for the definitions of the procedures. */
  48.  
  49. dev_proc_open_device(ega_open);
  50. dev_proc_close_device(ega_close);
  51. dev_proc_map_rgb_color(ega_map_rgb_color);
  52. dev_proc_map_color_rgb(ega_map_color_rgb);
  53. dev_proc_fill_rectangle(ega_fill_rectangle);
  54. dev_proc_tile_rectangle(ega_tile_rectangle);
  55. int ega_write_dot(P4(gx_device *, int, int, gx_color_index));
  56. dev_proc_copy_mono(ega_copy_mono);
  57. dev_proc_copy_color(ega_copy_color);
  58. dev_proc_get_bits(ega_get_bits);
  59.  
  60. /* Type for frame buffer pointers. */
  61. /* Note that 'far' gets defined as null on 32-bit systems. */
  62. typedef byte far *fb_ptr;
  63.  
  64. /* The device descriptor */
  65. typedef struct gx_device_ega_s gx_device_ega;
  66. struct gx_device_ega_s {
  67.     gx_device_common;
  68.     int raster;            /* frame buffer bytes per line */
  69.     int fb_seg_mult;        /* multiplier for segment part */
  70.                     /* of frame buffer pointer */
  71.     int fb_byte_mult;        /* multiplier for word part ditto */
  72. #define mk_fb_ptr(x, y)\
  73.   (fb_dev->fb_byte_mult == 0 ?\
  74.    (fb_ptr)MK_PTR(regen + (y) * (fb_dev->fb_seg_mult), (x) >> 3) :\
  75.    (fb_ptr)MK_PTR(regen + ((y) >> 4) * (fb_dev->fb_seg_mult),\
  76.          (((y) & 15) * fb_dev->fb_byte_mult) + ((x) >> 3)))
  77. /* Define the largest height that can be processed */
  78. /* within a single 64K segment.  If fb_dev->height > max_rop_height, */
  79. /* we may have to break up operations into pieces. */
  80.     unsigned max_rop_height;
  81.     int video_mode;
  82. };
  83.  
  84. /* Macro for casting gx_device argument */
  85. #define fb_dev ((gx_device_ega *)dev)
  86.  
  87. /* Procedure record */
  88. private gx_device_procs ega_procs = {
  89.     ega_open,
  90.     gx_default_get_initial_matrix,
  91.     gx_default_sync_output,
  92.     gx_default_output_page,
  93.     ega_close,
  94.     ega_map_rgb_color,
  95.     ega_map_color_rgb,
  96.     ega_fill_rectangle,
  97.     ega_tile_rectangle,
  98.     ega_copy_mono,
  99.     ega_copy_color,
  100.     gx_default_draw_line,
  101.     ega_get_bits,
  102.     gx_default_get_props,
  103.     gx_default_put_props
  104. };
  105.  
  106. /* Macro for creating instances */
  107. /* The initial parameters map an appropriate fraction of */
  108. /* the screen to an 8.5" x 11" coordinate space. */
  109. /* This may or may not be what is desired! */
  110. #define ega_device(dev_name, fb_raster, screen_height, aspect_ratio, video_mode)\
  111.    {    sizeof(gx_device_ega),\
  112.     &ega_procs,\
  113.     dev_name,\
  114.     fb_raster * 8, screen_height,\
  115.       (screen_height * (aspect_ratio)) / 11.0,    /* x density */\
  116.       screen_height / 11.0,        /* y density */\
  117.     no_margins,\
  118.        {    (rgb_max ? 3 : 1),    /* num_components */\
  119.         4,            /* depth */\
  120.         (rgb_max ? rgb_max : 1),    /* gray_max */\
  121.         rgb_max,\
  122.         3,            /* dither_gray */\
  123.         (rgb_max ? rgb_max + 1 : 0)    /* dither_rgb */\
  124.        },\
  125.     0,            /* not opened yet */\
  126.     fb_raster,\
  127.     (fb_raster & 15 ? fb_raster : fb_raster >> 4),\
  128.     (fb_raster & 15 ? fb_raster : 0),\
  129.     ((unsigned)(0xffff - fb_raster) / fb_raster),\
  130.     video_mode\
  131.    }
  132.  
  133. /* All the known instances */
  134.         /* EGA */
  135. gx_device_ega gs_ega_device =
  136.     ega_device("ega", 80, 350, 48.0/35.0, 0x10);
  137.         /* VGA */
  138. gx_device_ega gs_vga_device =
  139.     ega_device("vga", 80, 480, 1.0, 0x12);
  140.         /* Trident SuperVGA, 800x600, 16-color mode */
  141. gx_device_ega gs_tvga16_device =
  142.     ega_device("tvga16", 100, 600, 1.0, 0x5b);
  143.         /* Tseng Labs SuperVGA, 800x600, 16-color mode */
  144. gx_device_ega gs_tseng16_device =
  145.     ega_device("tseng16", 100, 600, 1.0, 0x29);
  146.         /* EIZO MDB-10 */
  147. gx_device_ega gs_mdb10_device =
  148.     ega_device("mdb10", 128, 768, 1.0, 0x37);
  149.  
  150. /* Define the color spectrum */
  151. #define black 0
  152. #define blue 1
  153. #define green 2
  154. #define cyan 3
  155. #define red 4
  156. #define magenta 5
  157. #define brown 6
  158. #define white 7
  159. #define dgray 8                /* dark gray is not very usable */
  160. #define lblue 9
  161. #define lgreen 10
  162. #define lcyan 11
  163. #define lred 12
  164. #define lmagenta 13
  165. #define yellow 14
  166. #define bwhite 15
  167.  
  168. /* Forward declarations */
  169. private int ega_get_mode();
  170. private void ega_set_mode(int);
  171.  
  172. /* Save the EGA mode */
  173. private int ega_save_mode = -1;
  174.  
  175. /* Reinitialize the EGA for text mode */
  176. int
  177. ega_close(gx_device *dev)
  178. {    if ( ega_save_mode >= 0 ) ega_set_mode(ega_save_mode);
  179.     return 0;
  180. }
  181.  
  182. /* Map a r-g-b color to an EGA color code. */
  183. gx_color_index
  184. ega_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  185.   gx_color_value b)
  186. {
  187. #define c12 (gx_max_color_value / 2)
  188. #define c13 (gx_max_color_value / 3)
  189. #define c23 (gx_max_color_value - c13)
  190. #define cmono() ((gx_color_index)mono_color[r >> (gx_color_value_bits - 2)])
  191. #if ega_bits_of_color == 0        /* monochrome */
  192.     static byte mono_color[4] = { black, white, white, bwhite };
  193.     return cmono();
  194. #else
  195. # if ega_bits_of_color == 1
  196.     static byte mono_color[4] = { black, white, white, bwhite };
  197.     if ( r == g && g == b )
  198.         return cmono();
  199.     return (gx_color_index)
  200.         ((r > c12 ? 4 : 0) |
  201.          (g > c12 ? 10 : 0) |
  202.          (b > c12 ? 1 : 0));
  203. # else
  204.     static byte g0[3][3] =
  205.      {{black,blue,lblue},{red,magenta,lmagenta},{lred,lmagenta,lmagenta}};
  206.     static byte g1[3][3] =
  207.      {{green,cyan,lcyan},{brown,white,lcyan},{yellow,yellow,lmagenta}};
  208.     static byte g2[3][3] =
  209.      {{lgreen,lgreen,lcyan},{lgreen,lgreen,lcyan},{yellow,yellow,bwhite}};
  210.     return (gx_color_index)
  211.         ((g >= c23 ? g2 : g >= c13 ? g1 : g0)
  212.          [r >= c23 ? 2 : r >= c13 ? 1 : 0]
  213.          [b >= c23 ? 2 : b >= c13 ? 1 : 0]);
  214. # endif
  215. #endif
  216. #undef c12
  217. #undef c13
  218. #undef c23
  219. }
  220.  
  221. /* Map a color code to r-g-b. */
  222. int
  223. ega_map_color_rgb(gx_device *dev, gx_color_index color,
  224.   gx_color_value prgb[3])
  225. {
  226. #define icolor (int)color
  227. #if rgb_max > 1
  228.     gx_color_value one =
  229.         (icolor & 8 ? gx_max_color_value : gx_max_color_value / 3);
  230. #else
  231. #  define one (gx_max_color_value / 2 + 1)
  232. #endif
  233.     prgb[0] = (icolor & 4 ? one : 0);
  234.     prgb[1] = (icolor & 2 ? one : 0);
  235.     prgb[2] = (icolor & 1 ? one : 0);
  236.     return 0;
  237. #undef one
  238. #undef icolor
  239. }
  240.  
  241. /* Macro for validating rectangle parameters x, y, w, h */
  242. #define validate_rect()\
  243.   if ( w <= 0 || h <= 0 ) return 0;\
  244.   if ( x < 0 || y < 0 || x + w > dev->width || y + h > dev->height ) return -1
  245.  
  246. /* ------ Internal routines ------ */
  247.  
  248. /* Read the device mode */
  249. private int
  250. ega_get_mode()
  251. {    registers regs;
  252.     regs.h.ah = 0xf;
  253.     int86(0x10, ®s, ®s);
  254.     return regs.h.al;
  255. }
  256.  
  257. /* Set the device mode */
  258. private void
  259. ega_set_mode(int mode)
  260. {    registers regs;
  261.     regs.h.ah = 0;
  262.     regs.h.al = mode;
  263.     int86(0x10, ®s, ®s);
  264. }
  265.  
  266. /* Structure for operation parameters. */
  267. /* Note that this structure is known to assembly code. */
  268. /* Not all parameters are used for every operation. */
  269. typedef struct rop_params_s {
  270.     fb_ptr dest;            /* pointer to frame buffer */
  271.     int draster;            /* raster of frame buffer */
  272.     byte far *src;            /* pointer to source data */
  273.     int sraster;            /* source raster */
  274.     int width;            /* width in bytes */
  275.     int height;            /* height in scan lines */
  276.     int shift;            /* amount to right shift source */
  277.     int invert;            /* 0 or -1 to invert source */
  278.     int data;            /* data for fill */
  279. } rop_params;
  280. typedef rop_params _ss *rop_ptr;
  281.  
  282. /* Assembly language routines */
  283.  
  284. #if USE_ASM
  285. void memsetcol(P1(rop_ptr)); /* dest, draster, height, data */
  286. #else
  287. #define memsetcol cmemsetcol
  288. private void
  289. cmemsetcol(rop_ptr rop)
  290. {    byte far *addr = rop->dest;
  291.     int yc = rop->height;
  292.     byte data = rop->data;
  293.     int draster = rop->draster;
  294.     while ( yc-- )
  295.      { byte_discard(*addr);
  296.        *addr = data;
  297.        addr += draster;
  298.      }
  299. }
  300. #endif
  301.  
  302. #if USE_ASM
  303. void memsetrect(P1(rop_ptr)); /* dest, draster, width, height, data */
  304. #else
  305. #define memsetrect cmemsetrect
  306. private void
  307. cmemsetrect(rop_ptr rop)
  308. {    int yc = rop->height;
  309.     int width = rop->width;
  310.     if ( yc <= 0 || width <= 0 ) return;
  311.        {    byte far *addr = rop->dest;
  312.         byte data = rop->data;
  313.         if ( width > 5 )    /* use memset */
  314.            {    int skip = rop->draster;
  315.             do
  316.                {    memset(addr, data, width);
  317.                 addr += skip;
  318.                }
  319.             while ( --yc );
  320.            }
  321.         else            /* avoid the fixed overhead */
  322.            {    int skip = rop->draster - width;
  323.             do
  324.                {    int cnt = width;
  325.                 do { *addr++ = data; } while ( --cnt );
  326.                 addr += skip;
  327.                }
  328.             while ( --yc );
  329.            }
  330.        }
  331. }
  332. #endif
  333.  
  334. #if USE_ASM
  335. void memrwcol(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  336. #  define memrwcol0(rop) memrwcol(rop)    /* same except shift = 0 */
  337. #else
  338. #  define memrwcol cmemrwcol
  339. #  define memrwcol0 cmemrwcol0
  340. private void
  341. cmemrwcol(rop_ptr rop)
  342. {    byte far *dp = rop->dest, *sp = rop->src;
  343.     int yc = rop->height;
  344.     int shift = rop->shift;
  345.     byte invert = rop->invert;
  346.     int sraster = rop->sraster, draster = rop->draster;
  347.     while ( yc-- )
  348.      { byte_discard(*dp);
  349.        *dp = ((*sp >> shift) + (*sp << (8 - shift))) ^ invert;
  350.        dp += draster, sp += sraster;
  351.      }
  352. }
  353. private void
  354. cmemrwcol0(rop_ptr rop)
  355. {    byte far *dp = rop->dest, *sp = rop->src;
  356.     int yc = rop->height;
  357.     byte invert = rop->invert;
  358.     int sraster = rop->sraster, draster = rop->draster;
  359.     if ( yc > 0 ) do
  360.      { byte_discard(*dp);
  361.        *dp = *sp ^ invert;
  362.        dp += draster, sp += sraster;
  363.      }
  364.     while ( --yc );
  365. }
  366. #endif
  367.  
  368. #if USE_ASM
  369. void memrwcol2(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  370. #else
  371. #define memrwcol2 cmemrwcol2
  372. private void
  373. cmemrwcol2(rop_ptr rop)
  374. {    byte far *dp = rop->dest, *sp = rop->src;
  375.     int yc = rop->height;
  376.     int shift = rop->shift;
  377.     byte invert = rop->invert;
  378.     int sraster = rop->sraster, draster = rop->draster;
  379.     while ( yc-- )
  380.      { byte_discard(*dp);
  381.        *dp = ((sp[1] >> shift) + (*sp << (8 - shift))) ^ invert;
  382.        dp += draster, sp += sraster;
  383.      }
  384. }
  385. #endif
  386.  
  387. /* Forward definitions */
  388. void fill_rectangle(P4(rop_ptr, int, int, int));
  389. void fill_row(P3(byte far *, int, int));
  390.  
  391. /* Define the device port and register numbers, and the regen map base */
  392. #define out2(port, index, data)\
  393.   (outportb(port, index), outportb((port)+1, data))
  394. #define seq_addr 0x3c4
  395. #define s_map 2
  396. #define set_s_map(mask) out2(seq_addr, s_map, mask)
  397. #define graph_addr 0x3ce
  398. #define g_const 0            /* set/reset */
  399. #define set_g_const(color) out2(graph_addr, g_const, color)
  400. #define g_const_map 1            /* enable set/reset */
  401. #define set_g_const_map(map) out2(graph_addr, g_const_map, map)
  402. #define g_function 3
  403. #define set_g_function(func) out2(graph_addr, g_function, func)
  404. #define g_read_plane 4
  405. #define set_g_read_plane(plane) out2(graph_addr, g_read_plane, plane)
  406. #  define gf_WRITE 0
  407. #  define gf_AND 8
  408. #  define gf_OR 0x10
  409. #  define gf_XOR 0x18
  410. #define g_mode 5
  411. #define set_g_mode(mode) out2(graph_addr, g_mode, mode)
  412. #  define gm_DATA 0
  413. #  define gm_FILL 2
  414. #define g_mask 8
  415. #define set_g_mask(mask) out2(graph_addr, g_mask, mask)
  416. #define regen 0xa000
  417.  
  418. /* Clean up after writing */
  419. #define dot_end()\
  420.   set_g_mask(0xff)            /* all bits on */
  421.  
  422. /* Initialize the EGA for graphics mode */
  423. int
  424. ega_open(gx_device *dev)
  425. {    if ( ega_save_mode < 0 ) ega_save_mode = ega_get_mode();
  426.     ega_set_mode(fb_dev->video_mode);
  427.     set_s_map(-1);            /* enable all maps */
  428.     return 0;
  429. }
  430.  
  431. /* Write a dot using the EGA color codes. */
  432. /* This doesn't have to be efficient. */
  433. int
  434. ega_write_dot(gx_device *dev, int x, int y, gx_color_index color)
  435. {    byte data = (byte)color;
  436.     return ega_copy_color(dev, &data, 1, 1, gx_no_bitmap_id, x, y, 1, 1);
  437. }
  438.  
  439. /* Macro for testing bit-inclusion */
  440. #define bit_included_in(x,y) !((x)&~(y))
  441.  
  442. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  443. /* Color = gx_no_color_index means transparent (no effect on the image). */
  444. int
  445. ega_copy_mono(gx_device *dev,
  446.   byte *base, int sourcex, int raster, gx_bitmap_id id,
  447.   int x, int y, int w, int h, gx_color_index izero, gx_color_index ione)
  448. {    rop_params params;
  449. #define czero (int)izero
  450. #define cone (int)ione
  451.     int dleft, count;
  452.     byte mask, rmask;
  453.     fb_ptr save_dest;
  454.     int other_color = -1;
  455.     validate_rect();
  456.     while ( h > fb_dev->max_rop_height )
  457.        {    unsigned mrh = fb_dev->max_rop_height;
  458.         int code = ega_copy_mono(dev, base, sourcex, raster, id,
  459.                 x, y, w, mrh, izero, ione);
  460.         if ( code != 0 ) return code;
  461.         base = (byte *)((byte huge *)base +
  462.                 raster * (long)mrh);
  463.         id = gx_no_bitmap_id;
  464.         y += mrh;
  465.         h -= mrh;
  466.        }
  467.     params.dest = mk_fb_ptr(x, y);
  468.     params.draster = fb_dev->raster;
  469.     params.src = base + (sourcex >> 3);
  470.     params.sraster = raster;
  471.     params.height = h;
  472.     params.shift = (x - sourcex) & 7;
  473.     /* Analyze the 16 possible cases: each of izero and ione may be */
  474.     /* 0, 0xf, transparent, or some other color. */
  475.     switch ( czero )
  476.        {
  477.     case no_color:
  478.         switch ( cone )
  479.            {
  480.         default:        /* (T, other) */
  481.             /* Must do 2 passes */
  482.             other_color = cone;
  483.             save_dest = params.dest;
  484.             /* falls through */
  485.         case 0:            /* (T, 0) */
  486.             set_g_function(gf_AND);
  487.             params.invert = -1;
  488.             break;
  489.         case 0xf:        /* (T, 0xf) */
  490.             set_g_function(gf_OR);
  491.             params.invert = 0;
  492.             break;
  493.         case no_color:        /* (T, T) */
  494.             return 0;    /* nothing to do */
  495.            }
  496.         break;
  497.     case 0:
  498.         params.invert = 0;
  499.         switch ( cone )
  500.            {
  501.         default:        /* (0, other) */
  502.             set_g_const(0);
  503.             set_g_const_map(cone ^ 0xf);
  504.             /* falls through */
  505.         case 0xf:        /* (0, 0xf) */
  506.             break;
  507.         case no_color:        /* (0, T) */
  508.             set_g_function(gf_AND);
  509.             break;
  510.            }
  511.         break;
  512.     case 0xf:
  513.         params.invert = -1;
  514.         switch ( cone )
  515.            {
  516.         case 0:            /* (0xf, 0) */
  517.             break;
  518.         default:        /* (0xf, other) */
  519.             set_g_const(0xf);
  520.             set_g_const_map(cone);
  521.             break;
  522.         case no_color:        /* (0xf, T) */
  523.             set_g_function(gf_OR);
  524.             /* falls through */
  525.            }
  526.         break;
  527.     default:
  528.         switch ( cone )
  529.            {
  530.         default:        /* (other, not T) */
  531.             if ( bit_included_in(czero, cone) )
  532.                {    set_g_const(czero);
  533.                 set_g_const_map(czero ^ cone ^ 0xf);
  534.                 params.invert = 0;
  535.                 break;
  536.                }
  537.             else if ( bit_included_in(cone, czero) )
  538.                {    set_g_const(cone);
  539.                 set_g_const_map(cone ^ czero ^ 0xf);
  540.                 params.invert = -1;
  541.                 break;
  542.                }
  543.             /* No way around it, fill with one color first. */
  544.             save_dest = params.dest;
  545.             fill_rectangle((rop_ptr)¶ms, x & 7, w, cone);
  546.             params.dest = save_dest;
  547.             set_g_function(gf_XOR);
  548.             set_s_map(czero ^ cone);
  549.             other_color = -2;    /* must reset s_map at end */
  550.             params.invert = -1;
  551.             break;
  552.         case no_color:        /* (other, T) */
  553.             /* Must do 2 passes */
  554.             other_color = czero;
  555.             save_dest = params.dest;
  556.             set_g_function(gf_AND);
  557.             params.invert = 0;
  558.             break;
  559.            }
  560.         break;
  561.        }
  562.     /* Actually copy the bits. */
  563.     dleft = 8 - (x & 7);
  564.     mask = 0xff >> (8 - dleft);
  565.     count = w - dleft;
  566.     if ( count < 0 )
  567.         mask -= mask >> w,
  568.         rmask = 0;
  569.     else
  570.         rmask = 0xff00 >> (count & 7);
  571.     /* params: dest, src, sraster, height, shift, invert */
  572.     /* Smashes params.src, params.dest, count. */
  573. copy:    set_g_mask(mask);
  574.     if ( params.shift == 0 )    /* optimize the aligned case */
  575.        {    /* Do left column */
  576.         memrwcol0((rop_ptr)¶ms);
  577.         /* Do center */
  578.         if ( (count -= 8) >= 0 )
  579.            {    set_g_mask(0xff);
  580.             do
  581.                {    params.src++, params.dest++;
  582.                 memrwcol0((rop_ptr)¶ms);
  583.                }
  584.             while ( (count -= 8) >= 0 );
  585.            }
  586.         /* Do right column */
  587.         if ( rmask )
  588.            {    params.src++, params.dest++;
  589.             set_g_mask(rmask);
  590.             memrwcol0((rop_ptr)¶ms);
  591.            }
  592.        }
  593.     else
  594.        {    /* Do left column */
  595.         int sleft = 8 - (sourcex & 7);
  596.         if ( sleft >= dleft )
  597.            {    /* Source fits in one byte */
  598.             memrwcol((rop_ptr)¶ms);
  599.            }
  600.         else if ( w <= sleft )
  601.            {    /* Source fits in one byte, thin case */
  602.             memrwcol((rop_ptr)¶ms);
  603.             goto fin;
  604.            }
  605.         else
  606.            {    memrwcol2((rop_ptr)¶ms);
  607.             params.src++;
  608.            }
  609.         /* Do center */
  610.         if ( (count -= 8) >= 0 )
  611.            {    set_g_mask(0xff);
  612.             do
  613.                {    params.dest++;
  614.                 memrwcol2((rop_ptr)¶ms);
  615.                 params.src++;
  616.                }
  617.             while ( (count -= 8) >= 0 );
  618.            }
  619.         /* Do right column */
  620.         if ( rmask )
  621.            {    set_g_mask(rmask);
  622.             params.dest++;
  623.             if ( count + 8 <= params.shift )
  624.                 memrwcol((rop_ptr)¶ms);
  625.             else
  626.                 memrwcol2((rop_ptr)¶ms);
  627.            }
  628.        }
  629. fin:    if ( other_color != -1 )
  630.        {    if ( other_color >= 0 )
  631.            {    /* Do the second pass on (T, other) or (other, T). */
  632.             count = w - dleft;
  633.             params.src = base + (sourcex >> 3);
  634.             params.dest = save_dest;
  635.             params.invert ^= -1;
  636.             set_s_map(other_color);
  637.             set_g_function(gf_OR);
  638.             other_color = -2;
  639.             goto copy;
  640.            }
  641.         else
  642.            {    /* Finished second pass, restore s_map */
  643.             set_s_map(-1);
  644.            }
  645.        }
  646.     set_g_function(gf_WRITE);
  647.     set_g_const_map(0);
  648.     dot_end();
  649.     return 0;
  650. #undef czero
  651. #undef cone
  652. }
  653.  
  654. /* Copy a color pixelmap.  This is just like a bitmap, */
  655. /* except that each pixel takes 4 bits instead of 1. */
  656. int
  657. ega_copy_color(gx_device *dev,
  658.   byte *base, int sourcex, int raster, gx_bitmap_id id,
  659.   int x, int y, int w, int h)
  660. {    byte *line = base + (sourcex >> 1);
  661.     unsigned lmask = 0x80 >> (x & 7);
  662.     int bitx = sourcex & 1;
  663.     int ex = w + bitx;
  664.     fb_ptr fbline;
  665.     validate_rect();
  666.     while ( h > fb_dev->max_rop_height )
  667.        {    unsigned mrh = fb_dev->max_rop_height;
  668.         int code = ega_copy_color(dev, base, sourcex, raster, id,
  669.                 x, y, w, mrh);
  670.         if ( code != 0 ) return code;
  671.         base = (byte *)((byte huge *)base +
  672.                 raster * (long)mrh);
  673.         id = gx_no_bitmap_id;
  674.         y += mrh;
  675.         h -= mrh;
  676.        }
  677.     fbline = mk_fb_ptr(x, y);
  678.     set_g_mode(gm_FILL);
  679.     outportb(graph_addr, g_mask);    /* first half of set_g_mask */
  680.     while ( --h >= 0 )
  681.     {    byte *bptr = line;
  682.         int px = bitx;
  683.         /* Turbo C will put an unsigned in a register, */
  684.         /* but not a byte! */
  685.         register unsigned mask = lmask;
  686.         fb_ptr fbptr = fbline;
  687.         do
  688.            {    byte_discard(*fbptr);    /* latch frame buffer data */
  689.             outportb(graph_addr+1, mask);    /* 2nd half of set_g_mask */
  690.             *fbptr = (px & 1 ? *bptr++ & 0xf : *bptr >> 4);
  691.             if ( (mask >>= 1) == 0 )
  692.                 mask = 0x80, fbptr++;
  693.            }
  694.         while ( ++px < ex );
  695.         line += raster;
  696.         fbline += fb_dev->raster;
  697.     }
  698.     set_g_mode(gm_DATA);
  699.     dot_end();
  700.     return 0;
  701. }
  702.  
  703. /* Fill a rectangle. */
  704. int
  705. ega_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  706.   gx_color_index color)
  707. {    rop_params params;
  708.     validate_rect();
  709.     while ( h > fb_dev->max_rop_height )
  710.        {    unsigned mrh = fb_dev->max_rop_height;
  711.         int code = ega_fill_rectangle(dev,
  712.             x, y, w, mrh, color);
  713.         if ( code != 0 ) return code;
  714.         y += mrh;
  715.         h -= mrh;
  716.        }
  717.     params.dest = mk_fb_ptr(x, y);
  718.     if ( h == 1 )
  719.        {    set_g_const((int)color);
  720.         fill_row(params.dest, x & 7, w);
  721.        }
  722.     else
  723.        {    params.draster = fb_dev->raster;
  724.         params.height = h;
  725.         fill_rectangle((rop_ptr)¶ms, x & 7, w, (int)color);
  726.        }
  727.     dot_end();
  728.     return 0;
  729. }
  730.  
  731. /* Tile a rectangle.  Note that the two colors must both be supplied, */
  732. /* i.e. neither one can be gx_no_color_index (transparent): */
  733. /* a transparent color means that the tile is colored, not a mask. */
  734. int
  735. ega_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  736.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  737.   int px, int py)
  738. #define zero (int)czero
  739. #define one (int)cone
  740. {    rop_params params;
  741.     int xmod, width_bytes;
  742.     int tile_height = tile->size.y;
  743.     int xbit;
  744.     int lcount;
  745.     int mask, rmask;
  746.     byte narrow;
  747.     byte again;
  748.     int const_bits, maps;
  749.     int ymod, yleft;
  750.     while ( h > fb_dev->max_rop_height )
  751.        {    int mod_height =
  752.             fb_dev->max_rop_height / tile_height * tile_height;
  753.         int code = ega_tile_rectangle(dev, tile,
  754.             x, y, w, mod_height, czero, cone, px, py);
  755.         if ( code != 0 ) return code;
  756.         y += mod_height;
  757.         h -= mod_height;
  758.        }
  759.     /* We only handle the easiest cases directly. */
  760.     if ( (tile->size.x & 7) || one == -1 || zero == -1 || px || py )
  761.         return gx_default_tile_rectangle(dev, tile, x, y, w, h,
  762.             czero, cone, px, py);
  763.     /* Following is similar to aligned case of copy_mono */    
  764.     validate_rect();
  765.     params.dest = mk_fb_ptr(x, y);
  766.     params.draster = fb_dev->raster;
  767.     params.sraster = tile->raster;
  768.     params.shift = 0;
  769.     xbit = x & 7;
  770.     /* Set up the graphics registers */
  771.     const_bits = (zero ^ one) ^ 0xf;
  772.     if ( const_bits )
  773.        {    set_g_const(zero);    /* either color will do */
  774.         set_g_const_map(const_bits);
  775.        }
  776.     if ( (maps = zero & ~one) != 0 )
  777.        {    set_s_map(maps += const_bits);
  778.         params.invert = -1;
  779.         again = one & ~zero;
  780.        }
  781.     else
  782.        {    maps = one & ~zero;
  783.         set_s_map(maps += const_bits);
  784.         params.invert = 0;
  785.         again = 0;
  786.        }
  787.     xmod = (x % tile->size.x) >> 3;
  788.     width_bytes = tile->size.x >> 3;
  789.     mask = 0xff >> xbit;
  790.     if ( w + xbit <= 8 )
  791.         mask -= mask >> w,
  792.         rmask = 0,
  793.         narrow = 1;
  794.     else
  795.        {    rmask = (0xff00 >> ((w + x) & 7)) & 0xff;
  796.         if ( xbit )    w += xbit - 8;
  797.         else        mask = 0, --xmod, --params.dest;
  798.         narrow = 0;
  799.        }
  800.     ymod = y % tile_height;
  801. tile:    yleft = tile_height - ymod;
  802.     params.src = tile->data + ymod * params.sraster + xmod;
  803.     lcount = h;
  804.     if ( narrow )            /* Optimize narrow case */
  805.        {    set_g_mask(mask);
  806.         if ( lcount > yleft )
  807.            {    params.height = yleft;
  808.             memrwcol0((rop_ptr)¶ms);
  809.             params.dest += yleft * params.draster;
  810.             params.src = tile->data + xmod;
  811.             params.height = tile_height;
  812.             lcount -= yleft;
  813.             while ( lcount >= tile_height )
  814.                {    memrwcol0((rop_ptr)¶ms);
  815.                 params.dest += tile_height * params.draster;
  816.                 lcount -= tile_height;
  817.                }
  818.            }
  819.         if ( lcount )
  820.            {    params.height = lcount;
  821.             memrwcol0((rop_ptr)¶ms);
  822.            }
  823.        }
  824.     else
  825.        {    fb_ptr line = params.dest;
  826.         int xpos = width_bytes - xmod;
  827.         while ( 1 )
  828.            {    int xleft = xpos;
  829.             int count = w;
  830.             params.height = (lcount > yleft ? yleft : lcount);
  831.             /* Do first byte, if not a full byte. */
  832.             if ( mask )
  833.                {    set_g_mask(mask);
  834.                 memrwcol0((rop_ptr)¶ms);
  835.                }
  836.             /* Do full bytes */
  837.             if ( (count -= 8) >= 0 )
  838.                {    set_g_mask(0xff);
  839.                 do
  840.                    {    if ( !--xleft )
  841.                         xleft = width_bytes,
  842.                         params.src -= width_bytes;
  843.                     ++params.src, ++params.dest;
  844.                     memrwcol0((rop_ptr)¶ms);
  845.                    }
  846.                 while ( (count -= 8) >= 0 );
  847.                }
  848.             /* Do last byte */
  849.             if ( rmask )
  850.                {    if ( !--xleft )
  851.                     xleft = width_bytes,
  852.                     params.src -= width_bytes;
  853.                 set_g_mask(rmask);
  854.                 ++params.src, ++params.dest;
  855.                 memrwcol0((rop_ptr)¶ms);
  856.                }
  857.             if ( (lcount -= params.height) == 0 ) break;
  858.             params.dest = line += params.height * params.draster;
  859.             params.src = tile->data + xmod;
  860.             yleft = tile_height;
  861.            }
  862.        }
  863.     /* Now do the second color if needed */
  864.     if ( again )
  865.        {    maps = again + const_bits;
  866.         set_s_map(maps);
  867.         again = 0;
  868.         params.dest = mk_fb_ptr(x, y);
  869.         if ( mask == 0 ) params.dest--;
  870.         params.invert = 0;
  871.         goto tile;
  872.        }
  873.     if ( maps != 0xf )
  874.         set_s_map(-1);
  875.     if ( const_bits )
  876.         set_g_const_map(0);
  877.     dot_end();
  878.     return 0;
  879. }
  880.  
  881. /* Read scan lines back from the frame buffer. */
  882. int
  883. ega_get_bits(gx_device *dev, int y, byte *data, uint size, int pad_to_word)
  884. {    /* We don't have to worry about padding, because we read back */
  885.     /* 4 bits per pixel and the frame buffer width is always */
  886.     /* a multiple of 8 pixels. */
  887.     uint bytes_per_row = dev->width >> 1;
  888.     uint count = min(dev->height - y, size / bytes_per_row);
  889.     int j, plane;
  890.     byte *drow = data;
  891.     memset(data, 0, size);
  892.     for ( j = count; j--; drow += bytes_per_row, y++ )
  893.       for ( plane = 0; plane < 4; plane++ )
  894.        {    fb_ptr src = mk_fb_ptr(0, y);
  895.         ushort *dest = (ushort *)drow;
  896.         int i;
  897.         /* Plane 0 is the least significant plane. */
  898.         /* We know we're on a little-endian machine.... */
  899.         static ushort spread4[16] =
  900.            {    0x0, 0x800, 0x8000, 0x8800,
  901.             0x8, 0x808, 0x8008, 0x8808,
  902.             0x80, 0x880, 0x8080, 0x8880,
  903.             0x88, 0x888, 0x8088, 0x8888
  904.            };
  905.         set_g_read_plane(plane);
  906.         for ( i = 0; i < dev->width; i += 8, src++, dest += 2 )
  907.            {    byte b = *src;
  908.             dest[0] = (dest[0] >> 1) + spread4[b >> 4];
  909.             dest[1] = (dest[1] >> 1) + spread4[b & 0xf];
  910.            }
  911.        }
  912.     return count;
  913. }
  914.  
  915. /* ------ Internal routines ------ */
  916.  
  917. /* Mask table for rectangle fill. */
  918. static byte rmask_tab[9] =
  919.    {    0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  920.    };
  921.  
  922. /* Fill a rectangle specified by pointer into frame buffer, */
  923. /* starting bit within byte, width, and height. */
  924. /* Smashes rop->dest. */
  925. private void
  926. fill_rectangle(register rop_ptr rop, int bit, int w, int color)
  927.   /* rop: dest, draster, height */
  928. {    set_g_const(color);
  929.     set_g_const_map(0xf);
  930.     if ( bit + w <= 8 )
  931.        {    /* Less than one byte */
  932.         set_g_mask(rmask_tab[w] >> bit);
  933.         memsetcol(rop);
  934.        }
  935.     else
  936.        {    byte right_mask;
  937.         if ( bit )
  938.            {    set_g_mask(0xff >> bit);
  939.             memsetcol(rop);
  940.             rop->dest++;
  941.             w += bit - 8;
  942.            }
  943.         if ( w >= 8 )
  944.            {    set_g_mask(0xff);    /* all bits */
  945.             rop->width = w >> 3;
  946.             memsetrect(rop);
  947.             rop->dest += rop->width;
  948.             w &= 7;
  949.            }
  950.         if ( (right_mask = rmask_tab[w]) != 0 )
  951.            {    set_g_mask(right_mask);
  952.             memsetcol(rop);
  953.            }
  954.        }
  955.     set_g_const_map(0);
  956. }
  957.  
  958. /* Fill a single row specified by pointer into frame buffer, */
  959. /* starting bit within byte, and width. */
  960. /* Caller has already set g_const. */
  961. #define r_m_w(ptr) (*(ptr))++        /* read & write, data irrelevant */
  962. private void
  963. fill_row(byte far *dest, int bit, int w)
  964.   /* rop: dest */
  965. {    set_g_const_map(0xf);
  966.     if ( bit + w <= 8 )
  967.        {    /* Less than one byte */
  968.         set_g_mask(rmask_tab[w] >> bit);
  969.         r_m_w(dest);
  970.        }
  971.     else
  972.        {    byte right_mask;
  973.         if ( bit )
  974.            {    set_g_mask(0xff >> bit);
  975.             r_m_w(dest);
  976.             dest++;
  977.             w += bit - 8;
  978.            }
  979.         if ( w >= 8 )
  980.            {    int byte_count = w >> 3;
  981.             set_g_mask(0xff);    /* all bits */
  982.             memset(dest, 0, byte_count);    /* data irrelevant */
  983.             dest += byte_count;
  984.             w &= 7;
  985.            }
  986.         if ( (right_mask = rmask_tab[w]) != 0 )
  987.            {    set_g_mask(right_mask);
  988.             r_m_w(dest);
  989.            }
  990.        }
  991.     set_g_const_map(0);
  992. }
  993.